﻿using UnityEngine;

namespace HotPlayer.Physic2D
{
    public static class GJKUtils
    {
        private static Simplex _simplex = new Simplex();

        public static Simplex cleanSimplex
        {
            get
            {
                _simplex.Clear();
                return _simplex;
            }
        }
        
        public static Simplex simplex => _simplex;

        public static Vector2 Support(Polygon2D shape1, Polygon2D shape2, Vector2 d)
        {
            // d is a vector direction (doesn't have to be normalized)
            // get points on the edge of the shapes in opposite directions
            var p1 = shape1.GetFarthestPointInDirection(d);
            var p2 = shape2.GetFarthestPointInDirection(-d);            
            // perform the Minkowski Difference, p1 - p2 is now a point in Minkowski space on the edge of the Minkowski Difference
            return p1 - p2;
        }
        
        public static Vector2 Support2(Polygon2D shape1, Polygon2D shape2, Vector2 d)
        {
            // d is a vector direction (doesn't have to be normalized)
            // get points on the edge of the shapes in opposite directions
            var p1 = shape1.GetNearestPointInDirection(d);
            var p2 = shape2.GetNearestPointInDirection(-d);            
            // perform the Minkowski Difference, p1 - p2 is now a point in Minkowski space on the edge of the Minkowski Difference
            return p1 - p2;
        }
    }
}